package com.syh.jn.scm.service.impl.supplier.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.system.SupplierAccount;
import com.syh.jn.scm.domain.supplier.entity.system.SupplierRole;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierAccountRepository;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierRoleRepository;
import com.syh.jn.scm.dto.operation.system.permission.PermissionVo;
import com.syh.jn.scm.dto.supplier.system.SupplierMenuVo;
import com.syh.jn.scm.dto.supplier.system.supplierRole.SupplierRoleMto;
import com.syh.jn.scm.dto.supplier.system.supplierRole.SupplierRoleSto;
import com.syh.jn.scm.dto.supplier.system.supplierRole.SupplierRoleVo;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.supplier.system.SupplierRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SupplierRoleServiceImpl extends AbstractServiceImpl<SupplierRoleSto, SupplierRoleMto, SupplierRoleVo, SupplierRole, Long> implements SupplierRoleService {

  @Autowired
  private SupplierRoleRepository supplierRoleRepository;

  @Override
  protected AggregateRepository<SupplierRole, Long> getAggregateRepository() {
    return this.supplierRoleRepository;
  }

  /**
   * 更新角色菜单
   *
   * @param roleId
   * @param menuIds
   */
  @Override
  public void updateRoleMenus(Long roleId, List<Long> menuIds) {
    SupplierRole role = supplierRoleRepository.getById(roleId);
    ExtAssert.notNull(role, "对象不存在");
    role.updateMenus(menuIds);
    supplierRoleRepository.update(role, true);
  }

  @Override
  public Long save(SupplierRoleSto sto) {
    SupplierRole supplierRole = new SupplierRole(sto);
    return supplierRoleRepository.add(supplierRole);
  }

  @Override
  public void update(SupplierRoleMto supplierRoleMto) {
    ExtAssert.notNull(supplierRoleMto, "对象为空");
    ExtAssert.notNull(supplierRoleMto.getId(), "id为空");
    SupplierRole supplierRole = supplierRoleRepository.getById(supplierRoleMto.getId());
    supplierRole.modify(supplierRoleMto);
    supplierRoleRepository.update(supplierRole);
  }

  @Override
  public void delete(Long id) {
    SupplierRole supplierRole = supplierRoleRepository.getById(id);
    if (supplierRole == null) {
      return;
    }
    supplierRole.destroy();
    supplierRoleRepository.removeById(id);
  }

  @Override
  public List<PermissionVo> getPermissionByRoleId(Long roleId) {
    return supplierRoleRepository.getPermissionByRoleId(roleId);
  }

  @Override
  public SupplierRoleVo getRole(Long accountId) {
    ExtAssert.notNull(accountId, "id为空");
    SupplierAccountRepository supplierAccountRepository = (SupplierAccountRepository) SpringContextUtils.getBean(SupplierAccountRepository.class);
    SupplierAccount supplierAccount = supplierAccountRepository.getById(accountId);
    if (supplierAccount == null) {
      throw new BusinessException("该账号无数据存在");
    }
    SupplierRole role = supplierRoleRepository.getById(supplierAccount.getRoleId());
    if (role == null) {
      return null;
    }
    SupplierRoleVo roleVo = new SupplierRoleVo();
    ExtBeanUtils.copyProperties(role, roleVo);
    return roleVo;
  }

  /**
   * 获取角色菜单
   *
   * @param wrapper
   */
  @Override
  public List<SupplierMenuVo> getMenus(Wrapper wrapper) {
    return this.supplierRoleRepository.getMenus(wrapper);
  }

  @Override
  public List<SupplierRoleVo> getAllRolesWithPermission() {
    List<SupplierRoleVo> roles = supplierRoleRepository.getAllRolesWithPermission();
    return roles;
  }

  @Override
  public List<SupplierRoleVo> getRolesByPermissionId(Long perId) {
    return supplierRoleRepository.getRolesByPermissionId(perId).stream().map(role -> {
      SupplierRoleVo roleVo = new SupplierRoleVo();
      ExtBeanUtils.copyProperties(role, roleVo);
      return roleVo;
    }).collect(Collectors.toList());
  }
}
